package light.head.dao.mobile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.Task;

@Service
public class MobileTaskDAO extends BaseDAO<Task, Integer> {

	public RESULT findByMap(Integer oc, Map<String, String> map, PageBean pb) {
		
		if (null == oc) return null;
		
		StringBuffer hql = new StringBuffer("FROM Task t WHERE t.oc = ?");
		List<Object> parm = new ArrayList<Object>();
		
		parm.add(oc);
		
		if (null != map) {
			
			String start = map.get(Parm.START);
			String end = map.get(Parm.END);
			String ru = map.get(Parm.RU);
			String du = map.get(Parm.DU);
			String user = map.get(Parm.USER);
			String title = map.get(Parm.TITLE);
			if (isNotNull(start) && isNotNull(end)) {
				hql.append(" AND t.start BETWEEN ? AND ?");
				parm.add(DateUtils.getDateByStr(start, "yyyy-MM-dd"));
				parm.add(DateUtils.getDateByStr(end, "yyyy-MM-dd"));
			}
			if (isNotNull(ru)) {
				hql.append(" AND t.ru = ?");
				parm.add(Integer.parseInt(ru));
			}
			if (isNotNull(du)) {
				hql.append(" AND t.du = ?");
				parm.add(Integer.parseInt(du));
			}
			if (isNotNull(user)) {
				hql.append(" AND t.user = ?");
				parm.add(Integer.parseInt(user));
			}
			if (isNotNull(title)) {
				hql.append(" AND t.title LIKE ?");
				parm.add("%" + title + "%");
			}
		}

		hql.append(" AND t.deleted = 0 or t.deleted is null ");
		hql.append(" ORDER BY t.code DESC");
		return findToPageBean(hql.toString(), pb, parm);
	}
}
